/**
 * 作者: hxl
 * 日期: 2025-04-24 16:57:53
 * 备注: 用户联盟乱斗信息表
 */
package com.motu.monstercity.server.game.userdata;

import com.motu.monstercity.server.game.base.AllParam;
import com.motu.monstercity.server.game.commondata.bag.ItemId;
import com.motu.monstercity.server.game.log.LogType;
import com.motu.monstercity.server.game.log.MotuLogManager;
import com.motu.monstercity.server.game.logic.GameUser;
import com.motu.vertx.module.utility.toolset.TimeUtils;
import io.vertx.core.json.JsonObject;

public class UserUnionScuffleInfoWeek extends UserUnionScuffleInfoWeekModel {
    public static final byte YES = 1;
    public static final byte NO = 0;
    public UserUnionScuffleInfoWeek() {
        super();
    }

    public JsonObject insert() {
        putWeekResetTime(TimeUtils.getCurTime());
        return super.insert();
    }

    public void reset() {
        long curTime = TimeUtils.getCurTime();
        boolean isUpdate = false;
        if (!TimeUtils.isToday(getDayResetTime())) {
            putVideoWatch(0);
            putDayResetTime(curTime);
            isUpdate = true;
        }
//        if (!TimeUtils.isSameWeek(curTime, getWeekResetTime())) {
//            isUpdate  = true;
//        }
        if (getEnergyRecoverTime() == 0) {
            putEnergy(AllParam.UNION_SCUFFLE_ENERGY_MAX);
            putEnergyRecoverTime(curTime);
            isUpdate = true;
        } else if (curTime - getEnergyRecoverTime() >= AllParam.UNION_SCUFFLE_ENERGY_RECOVER_TIME) {
            if (getEnergy() < AllParam.UNION_SCUFFLE_ENERGY_MAX) {
                long deltaTime = curTime - getEnergyRecoverTime();
                int deltaEnergy = (int) (deltaTime / AllParam.UNION_SCUFFLE_ENERGY_RECOVER_TIME);
                long recoverTime = getEnergyRecoverTime() + (long) deltaEnergy * AllParam.UNION_SCUFFLE_ENERGY_RECOVER_TIME;
                int curEnergy = getEnergy() + deltaEnergy;
                if (curEnergy > AllParam.UNION_SCUFFLE_ENERGY_MAX) {
                    deltaEnergy  = AllParam.UNION_SCUFFLE_ENERGY_MAX - getEnergy();
                }
                UserInfo userInfo = GameUser.getUserInfo(getId());
                addEnergy(userInfo, deltaEnergy, LogType.UNION_SCUFFLE_AUTO_RECOVER_ENERGY);
                putEnergyRecoverTime(recoverTime);
                isUpdate = true;
            } else {
                putEnergyRecoverTime(curTime);
                isUpdate = true;
            }
        }
        if (isUpdate) {
            update();
        }
    }

    public void addEnergy(UserInfo userInfo, int addValue, Object... params) {
        if (addValue <= 0) {
            return;
        }
        int balance = getEnergy();
        super.addEnergy(addValue);
        if (params.length > 0) {
            MotuLogManager.logItemIncome(userInfo, ItemId.SCUFFLE_ENERGY, 0, balance, addValue, params);
        }
    }

    public void subEnergy(UserInfo userInfo, int subValue, Object... params) {
        if (subValue <= 0) {
            return;
        }
        int balance = getEnergy();
        if (balance < subValue) {
            subValue = balance;
        }
        super.addEnergy(-subValue);
        if (params.length > 0) {
            MotuLogManager.logItemConsume(userInfo, ItemId.SCUFFLE_ENERGY, 0, balance, subValue, params);
        }
    }

    public void resetWeekData() {
        putSingleRankReward(0);
        putUnionRankReward(0);
        putBuyHigh(NO);
        putBuySpecial(NO);
        putBeatNum(0);
        putNormalPassReward(0);
        putHighPassRewad(0);
        putSpecialPassReward(0);
        putScore(0);
    }

}
